package priv.pfz.qatool;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import priv.pfz.qatool.enums.QAToolParamType;
import priv.pfz.qatool.model.QT;
import priv.pfz.qatool.model.QTParam;
import priv.pfz.qatool.util.ParseUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.Writer;
import java.text.ParseException;


/**
 * Created by fangzhou.peng on 2017/8/3.
 */
public class QTContext {

    private static final Logger logger = Logger.getLogger(QTContext.class);

    private static final String FILE_PATH = "/data/log/qatool/files";

    static {
        checkFileDir();
    }

    final private QT qt;
    final private HttpServletRequest request;
    final private HttpServletResponse response;
    final private Writer out;
    private String method;
    private MultipartRequest multipartRequest;

    public QTContext(QT qt, HttpServletRequest request, HttpServletResponse response, Writer out) {
        this.qt = qt;
        this.request = request;
        this.response = response;
        this.out = out;
        this.method = request.getParameter("method");
        if (StringUtils.isNotEmpty(method)) {
            try {
                this.multipartRequest = new MultipartRequest(request, FILE_PATH, 104857600, "UTF-8", new DefaultFileRenamePolicy());
            } catch (Exception e) {

            }
        }
    }

    public QT qt() {
        return qt;
    }

    public HttpServletRequest request() {
        return request;
    }

    public HttpServletResponse response() {
        return response;
    }

    public Writer out() {
        return out;
    }

    public String method() {
        return method;
    }

    public boolean isInvoke() {
        return StringUtils.isNotEmpty(method);
    }

    public <T> T get(int index, QTParam param) throws ParseException {
        String arg = StringUtils.isEmpty(param.getName()) ? "arg" + index : param.getName();
        QAToolParamType type = param.getType();
        if (type == QAToolParamType.FILE) {
            File file = multipartRequest.getFile(arg);
            if (file != null) {
                file.deleteOnExit();
            }
            return (T) file;
        } else {
            String value;
            if (multipartRequest != null) {
                value = multipartRequest.getParameter(arg);
            } else {
                value = request.getParameter(arg);
            }
            if (StringUtils.isEmpty(value)) {
                value = param.getDefaultValue();
            }
            if (type == QAToolParamType.DATE_TIME_PICKER) {
                return (T) (Long) ParseUtil.parseTime(value);
            } else if (type == QAToolParamType.DATE_PICKER) {
                return (T) (Long) ParseUtil.parseDate(value);
            } else if (type == QAToolParamType.CHECKBOX) {
                if ("on".equals(value)) {
                    value = "true";
                }
                return (T) ParseUtil.parseString(value, param.getClazz());
            } else {
                return (T) ParseUtil.parseString(value, param.getClazz());
            }
        }
    }

    private static void checkFileDir() {
        File file = new File(FILE_PATH);
        if (file.exists()) {
            if (!file.isDirectory()) {
                throw new RuntimeException("要创建文件目录的地方存在一个同名文件");
            }
        } else {
            if (!file.mkdirs()) {
                throw new RuntimeException("创建目录失败");
            }
        }
    }

}
